package com.das.mechanic_base.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.das.mechanic_base.R;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3ToastUtils;
import com.umeng.analytics.MobclickAgent;

public class X3BottomAddThingDialog extends X3BaseBottomSheetDialog implements View.OnClickListener,TextWatcher {

    private TextView tv_affirm;
    private TextView tv_cancel;
    private TextView tv_clear;
    private TextView tv_num;
    private EditText et_input;
    private int receiveId;

    public X3BottomAddThingDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_clear = getView(R.id.tv_clear);
        tv_cancel = getView(R.id.tv_cancel);
        tv_affirm = getView(R.id.tv_affirm);
        tv_num = getView(R.id.tv_num);
        et_input = getView(R.id.et_input);

        et_input.addTextChangedListener(this);
        tv_cancel.setOnClickListener(this);
        tv_clear.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_add_thing_dialog;
    }

    public void setContentText(String content) {
        if (X3StringUtils.isEmpty(content)) {
            return;
        }
        et_input.setText(content);
        et_input.setSelection(content.length());
    }

    @Override
    public void show() {
        super.show();
        tv_clear.performClick();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_affirm) {/**
         * 确定添加物品名称
         */
            String thing = et_input.getText().toString();
            if (X3StringUtils.isEmpty(thing)) {
                X3ToastUtils.showMessage(mContext.getString(R.string.x3_please_input_thing));
                return;
            }
            if (iOnAddThing != null) {
                iOnAddThing.iOnAddCarThing(thing);
            }
            dismiss();
        } else if (id == R.id.tv_clear) {/**
         * 清空车主内容
         */
            et_input.setText("");
        } else if (id == R.id.tv_cancel) {
            dismiss();
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        String content = editable.toString();

        //tv_clear.setVisibility(StringUtils.isEmpty(content) ? View.GONE : View.VISIBLE);
        if (X3StringUtils.isEmpty(content)) {
            tv_num.setText("0/30");
        }else {
            tv_num.setText(content.length() + "/30");
        }
    }

    IOnAddThing iOnAddThing;

    public void setiOnAddThing(IOnAddThing iOnAddThing) {
        this.iOnAddThing = iOnAddThing;
    }

    public interface IOnAddThing {
        void iOnAddCarThing(String thing);
    }

    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("初检信息添加物品弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("初检信息添加物品弹窗");
    }
}
